$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var orderId = Common.fn.getUrlParam('oid');
    if (!orderId) {
        location.href = 'or_buy_auction.html';
    }
    var result = Model.order.orderDetail({
        orderId: orderId
    });
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    // 设置页面信息
    $('.order-status').text(result.data[0].order.state);
    $('.order-id').text(result.data[0].order.orderId);
    $('.order-count').text('￥' + result.data[0].order.price);
    $('.user-nickname').text(result.data[0].user.nickanme);
    $('.user-phone').text(result.data[0].user.phone);
    $('.user-address').text(result.data[0].user.address);
    $('.portrait-img').attr('src', result.data[0].goods.picture);
    $('.order-good-title').text(result.data[0].goods.content);
    $('.goods-price').text(result.data[0].goods.topPrice);
    switch (result.data[0].order.state) {
        case '竞拍中':
            $('.apply-refund-btn').css('display', 'none');
            $('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
            break;
        case '待付款':
            $('.apply-refund-btn').css('display', 'none');
            $('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
            $('.total-price').text('￥' + parseInt(result.data[0].order.price - 10));
            $('.order-footer-pay').css('display', 'block');
            $('.order-footer-pay-btn').attr('data-id', result.data[0].order.orderId);
            break;
        case '待发货':
            $('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
            $('.apply-refund-btn').attr('data-id', result.data[0].order.orderId);
            $('.footer-cancel-send').css('display', 'block');
            $('.sure-send-btn').attr('data-id', result.data[0].order.orderId);
            break;
        case '待收货':
            $('.wuliu-order').text(result.data[0].order.express + ' ' + result.data[0].order.expressOrder);
            $('.s-footer').css('display', 'none');
            break;
        case '已完成':
            $('.apply-refund-btn').css('display', 'none');
            $('.wuliu-order').text(result.data[0].order.express + ' ' + result.data[0].order.expressOrder);
            $('.s-footer').css('display', 'none');
            break;
        case '退款中':
            $('.wuliu-order').text(result.data[0].order.express + ' ' + result.data[0].order.expressOrder);
            $('.apply-refund-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
            $('.footer-cancel-pay').css('display', 'block');
            $('.sure-return-btn').attr('data-id', result.data[0].order.orderId);
            $('.false-return-btn').attr('data-id', result.data[0].order.orderId);
            break;
        default:
            $('.apply-refund-btn').css('display', 'none');
            $('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
            break;
    }
    loading.hide();
});

// 复制订单编号
$('#copy-orderId-btn').on('click', function () {
    var orderId = $('.order-id').text();
    var clipboard = new Clipboard("#copy-orderId-btn", {
        text: function text() {
            return orderId;
        }
    });
    clipboard.on('success', function (e) {
        weui.toast('已成功复制到剪贴板', {
            duration: 2000,
            callback: function callback() {}
        });
    });
    clipboard.on('error', function (e) {
        weui.topTips('复制剪贴板失败，请手动复制');
    });
});

// 复制快递信息
$('#wuliu-copy-btn').on('click', function () {
    var wuliu = $('.wuliu-order').text();
    var clipboard = new Clipboard("#wuliu-copy-btn", {
        text: function text() {
            return wuliu;
        }
    });
    clipboard.on('success', function (e) {
        weui.toast('已成功复制到剪贴板', {
            duration: 2000,
            callback: function callback() {}
        });
    });
    clipboard.on('error', function (e) {
        weui.topTips('复制剪贴板失败，请手动复制');
    });
});

// 确认发货按钮
$(document).on('click', '.sure-send-btn', function () {
    $('.order-form').removeClass('hide');
    $('.order-btn-submit').attr('data-id', $(this).attr('data-id'));
});

// 确认退款按钮
$(document).on('click', '.sure-return-btn', function () {
    var orderId = $(this).attr('data-id');
    Common.fn.sureForReturn(orderId);
});

// 拒绝退款按钮
$(document).on('click', '.false-return-btn', function () {
    var orderId = $(this).attr('data-id');
    Common.fn.falseForReturn(orderId);
});

// 取消按钮
$(document).on('click', '.order-btn-quit', function () {
    $('.order-form').addClass('hide');
});

// 选择快递信息
$(document).on('click', '#express-company', function() {
    $('.classify-block').css('display', 'block');
});

// 选择快递关闭按钮
$(document).on('click', '.classify-close-btn', function () {
    $('.classify-block').css('display', 'none');
});

// 选择快递公司
$(document).on('click', '.start-classify-item', function() {
    var id = $(this).attr('data-id');
    $('.classify-block').css('display', 'none');
    $('#express-company').val(id);
});

// 确认按钮
$(document).on('click', '.order-btn-submit', function () {
    var company = $.trim($('#express-company').val()),
        number = $.trim($('#express-number').val()),
        id = $(this).attr('data-id');
    if(company == '' || number == '') {
        weui.alert('请填写快递信息');
        return false;
    }
    var confirms = weui.confirm(
        "确认发货吗？",
        function () {
            var result = Model.order.sendGoods({
                orderId: id,
                express: company,
                expressOrder: number
            });
            if (result.code) {
                alert(result.message);
                return false;
            }
            alert(result.data);
            location.href = 'or_seller_deliver.html';
        },
        function () { }
    );
});